<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="学习For循环引起的问题看书对于每个前端人员都非常重要，它能最直接的提供给我们知识，并且总能给我们惊喜。今天看到关于循环语句和分支语句， 而书中给了一种我所不熟悉的分支语句">
<meta property="og:type" content="article">
<meta property="og:title" content="JS学习笔记（一）">
<meta property="og:url" content="https://inkgn.gitee.io/2018/11/02/newclass1/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="学习For循环引起的问题看书对于每个前端人员都非常重要，它能最直接的提供给我们知识，并且总能给我们惊喜。今天看到关于循环语句和分支语句， 而书中给了一种我所不熟悉的分支语句">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(6).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(7).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(8).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(9).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(10).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(1).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(2).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(3).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(4).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(5).png">
<meta property="article:published_time" content="2018-11-02T05:44:59.000Z">
<meta property="article:modified_time" content="2018-11-02T05:44:59.000Z">
<meta property="article:author" content="yinks">
<meta property="article:tag" content="JS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(6).png">

<link rel="canonical" href="https://inkgn.gitee.io/2018/11/02/newclass1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>JS学习笔记（一） | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2018/11/02/newclass1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS学习笔记（一）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-11-02 13:44:59" itemprop="dateCreated datePublished" datetime="2018-11-02T13:44:59+08:00">2018-11-02</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JS/" itemprop="url" rel="index"><span itemprop="name">JS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="学习For循环引起的问题"><a href="#学习For循环引起的问题" class="headerlink" title="学习For循环引起的问题"></a>学习For循环引起的问题</h4><p>看书对于每个前端人员都非常重要，它能最直接的提供给我们知识，并且总能给我们惊喜。今天看到关于循环语句和分支语句， 而书中给了一种我所不熟悉的分支语句</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">If（条件）&#123;</span><br><span class="line">    条件满足时执行</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    条件不满足时执行</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>像这种简单的写法我们有更加简洁且优雅的写法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">条件 ?<span class="string">&#x27;条件满足时执行&#x27;</span>：<span class="string">&#x27;条件不满足时执行&#x27;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>其实这种结构并不难以理解，问题在于下面代码</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(6).png" alt="js"></p>
<p>我们可以看到在两个for循环内有一个变量 res 代码段为:</p>
<p>Res += （i*j）% 8 ？’ ’ : ’ * ’;</p>
<p>而整体结构还是很好理解的但是唯一让我疑惑的时（i*j）% 8 </p>
<p>根据运算 i = 1;  j = 1;  1%8 == 1 ; 发现条件为一个数值，我不能理解<br>这个数值怎么作为条件判断？不能理解我们就来敲一下！Console.log输出为：</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(7).png" alt="js"></p>
<p>输出后更加让我迷茫了，条件居然满足了？<br>不死心的我又尝试了其他的</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(8).png" alt="js"></p>
<p>第一次用了3%3条件判断恰好是余数0；结果显示结果为条件不满足。而第二次<br>我重新定义的一个变量var i = 3;   i &lt; 5 ? ‘shi’ : ‘cuo’;  这个有着正常的条件，所以输出正常。我开始有点慢慢理解了，于是我又继续试验….</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(9).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(10).png" alt="js"></p>
<p>看到这大家应该都明白了，是的，布尔值！这种数字和字符串将被转换成布尔值，<br>根据数据类型的转换除去那六种特殊的之外（空字符串‘’，  null，  undefined， 数字0，数字类型NaN,  还有布尔值false）其余的都将被转换成true；</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(1).png" alt="js"></p>
<p>然后我们又发现一个有趣的现象，未加引号的字符会被当成未声明未赋值的对象（abjcet;）<br>输出是报错！，而声明后的未赋值对象输出正常 。</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(2).png" alt="js"></p>
<p>而经过试验不管是声明过还是没用声明过得它的数据类型都是undefined；<br>那么为什么同样的类型会有报错呢？（原因：js中任何对象执行过后才表示存在）但是经过上面的例子<br>可以猜想到未声明未赋值的对象是无法执行的 ，声明后（已存在）的未赋值对象输出正常</p>
<h4 id="外加一条C3属性Z-index"><a href="#外加一条C3属性Z-index" class="headerlink" title="外加一条C3属性Z-index"></a>外加一条C3属性Z-index</h4><p>z-index 属性指定一个元素的堆叠顺序。<br>拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。</p>
<p>关于这把我整的够呛，我们平时也会有，布局上没有问题可是效果图上却出现异常，比如这次我导入了一堆div，宽高都是40px; 基于这些盒子相对定位，在这些盒子上分别绝对定位宽高都为80px的盒子并且都隐藏，我要的效果是当鼠标经过某个div后，该div相对的绝对定位盒子‘显示’！</p>
<p>（错误效果图如下）</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(3).png" alt="js"></p>
<p>这种异常层级关系，如果单纯的从css上找出问题，很困难(我是没有发现错误)<br>所以像这样的或者更加复杂的页面布局我们可以采用z-index;属性。</p>
<p>Z-index 值可以从1~999，取值越高层级越高。</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(4).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/%20(5).png" alt="js"></p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2018/11/02/newclass1/" title="JS学习笔记（一）">https://inkgn.gitee.io/2018/11/02/newclass1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JS/" rel="tag"># JS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2000/11/08/hello-world/" rel="prev" title="Hello World">
      <i class="fa fa-chevron-left"></i> Hello World
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/11/08/class/" rel="next" title="自学方法论">
      自学方法论 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%A6%E4%B9%A0For%E5%BE%AA%E7%8E%AF%E5%BC%95%E8%B5%B7%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">学习For循环引起的问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%96%E5%8A%A0%E4%B8%80%E6%9D%A1C3%E5%B1%9E%E6%80%A7Z-index"><span class="nav-number">2.</span> <span class="nav-text">外加一条C3属性Z-index</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
